package com.kawa.kemanage.controller.api;

import com.kawa.kemanage.entity.SystemNotice;
import com.kawa.kemanage.entity.User;
import com.kawa.kemanage.service.SystemNoticeService;
import com.kawa.kemanage.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/systemNotice")
public class SystemNoticeController {
    @Autowired
    private SystemNoticeService systemNoticeService;
    /*
    查询
     */
    @RequestMapping("/NoticeList")
    public Result<List<SystemNotice>> getSystemNotices() {
        return Result.success(systemNoticeService.selectAll());
    }

    /**
     *
     * @param userId
     * @return
     */
    @GetMapping("/selectUserNoticeByUserId/{userId}")
    public Result selectByUserId(@PathVariable Integer userId,
                                 @RequestParam(value = "pageNum",defaultValue = "1") Integer page,
                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer size){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("List",systemNoticeService.selectByUserId(userId,page,size));
        return Result.success(resultMap);
    }


//    @RequestMapping("/searchNotice")
//    public Result searchNotice(@RequestParam String userName) {
//        return Result.success(systemNoticeService.searchNotice(userName));
//    }
//@RequestMapping("/searchNotice")
//public Result searchNotice(@RequestParam String userName) {
//    try {
//        String result = systemNoticeService.searchNotice(userName);
//        if (result != null && !result.isEmpty()) {
//            return Result.success(result);
//        }
//        return Result.error("未找到相关用户");
//    } catch (Exception e) {
//        return Result.error("查询失败: " + e.getMessage());
//    }
//}
@RequestMapping("/searchNotice")
public Result searchNotice(@RequestParam String userName) {
    try {
        List<User> result = systemNoticeService.searchNotice(userName);
        if (result != null && !result.isEmpty()) {
            return Result.success(result);
        }
        return Result.error("未找到相关用户");
    } catch (Exception e) {
//        logger.error("查询用户信息异常: {}", userName, e);
        return Result.error("查询失败: " + e.getMessage());
    }
}
    /*
    添加
     */
    @RequestMapping("/add")
    public Result addSystemNotice(@RequestBody SystemNotice systemNotice) {
        boolean result = systemNoticeService.addNotice(systemNotice);
            if (result) {
                return Result.success();
        } else {
        return Result.error("发布失败！");
    }}
    /*
    修改
     */
    @RequestMapping("/update")
    public Result updateSystemNotice(@RequestBody SystemNotice systemNotice) {
        boolean result =systemNoticeService.updateNotice(systemNotice);
        if (result){
            return  Result.success("修改成功");
        }else {
            return  Result.error("修改失败");
        }
}

    @PutMapping("/updateStatus") // 使用更语义化的注解
    public Result<String> updateNewsStatus(@RequestBody Map<String, Object> requestBody) {
        try {
            Integer notificationId = Integer.valueOf(requestBody.get("notification_id").toString());
            String status = requestBody.get("status").toString();
            Date readTime = status.equals("read") ? new Date() : null;

            int result = systemNoticeService.updateNewsStatusAndReadTime(
                    notificationId, readTime, status);

            return Result.success(result > 0 ? "更新成功" : "更新失败");
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    /*
    删除
     */
    @RequestMapping("/delete/{notification_id}")
    public Result deleteSystemNotice(@PathVariable Integer notification_id) {
        try {
            int deleteNum = systemNoticeService.deleteNotice(notification_id);
            if (deleteNum > 0) {
                return Result.success("删除成功");
            }
            return  Result.error("删除失败!!!");
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据用户id查询评论消息
     */
    @RequestMapping("/selectNoticeByUserId/{user_id}")
    public Result selectNoticeByUserId(@PathVariable Integer user_id){
        return Result.success(systemNoticeService.selectNoticeByUserId(user_id));
    }
}
